import 'package:flutter/material.dart';

/// Animaiton基础使用
/// 使用AnimationWiget可省略手动setState()方法

/// 第一种直接使用
// class AnimationPage1 extends StatefulWidget {
//   @override
//   _AnimationPage1State createState() => _AnimationPage1State();
// }

// class _AnimationPage1State extends State<AnimationPage1>
//     with SingleTickerProviderStateMixin {
//   AnimationController controller;
//   Animation animation;
//   @override
//   void initState() {
//     super.initState();

//     controller = AnimationController(
//         duration: Duration(milliseconds: 2000), vsync: this);
//     animation = new Tween(begin: 0.0, end: 300.0).animate(controller)
//       ..addListener(() {
//         setState(() {});
//       });

//     controller.forward();
//   }

//   @override
//   Widget build(BuildContext context) {
//     return Center(
//         child: Container(
//       width: animation.value,
//       height: animation.value,
//       child: FlutterLogo(),
//     ));
//   }

//   @override
//   void dispose() {
//     controller.dispose();
//     super.dispose();
//   }
// }

/// 第二种使用AnimatedWidget
// class AnimationPage1 extends StatefulWidget {
//   @override
//   _AnimationPage1State createState() => _AnimationPage1State();
// }

// class _AnimationPage1State extends State<AnimationPage1>
//     with SingleTickerProviderStateMixin {
//   AnimationController controller;
//   Animation animation;
//   @override
//   void initState() {
//     super.initState();

//     controller = AnimationController(
//         duration: Duration(milliseconds: 2000), vsync: this);
//     animation = new Tween(begin: 0.0, end: 300.0).animate(controller);
//     controller.forward();
//   }

//   @override
//   Widget build(BuildContext context) {
//     return Center(
//       child: AnimationLogo(
//         animation: animation,
//       ),
//     );
//   }

//   @override
//   void dispose() {
//     controller.dispose();
//     super.dispose();
//   }
// }

// class AnimationLogo extends AnimatedWidget {
//   AnimationLogo({Key key, Animation animation})
//       : super(key: key, listenable: animation);
//   @override
//   Widget build(BuildContext context) {
//     final Animation animation = listenable;
//     return Center(
//       child: Container(
//         width: animation.value,
//         height: animation.value,
//         child: FlutterLogo(),
//       ),
//     );
//   }
// }

/// 第三种使用AnmationBuilder
// class GrowTransiton extends StatelessWidget {
//   GrowTransiton({this.child, this.animation});

//   final Widget child;
//   final Animation animation;

//   @override
//   Widget build(BuildContext context) {
//     return Center(
//       child: AnimatedBuilder(
//         animation: animation,
//         builder: (BuildContext context, Widget child) {
//           return Container(
//             width: animation.value,
//             height: animation.value,
//             child: child,
//           );
//         },
//         child: child,
//       ),
//     );
//   }
// }

// class AnimationPage1 extends StatefulWidget {
//   @override
//   _AnimationPage1State createState() => _AnimationPage1State();
// }

// class _AnimationPage1State extends State<AnimationPage1>
//     with SingleTickerProviderStateMixin {
//   Animation animation;
//   AnimationController controller;
//   @override
//   void initState() {
//     super.initState();
//     controller = AnimationController(
//         duration: Duration(milliseconds: 2000), vsync: this);
//     animation = Tween(begin: 0.0, end: 300.0).animate(controller);
//     controller.forward();
//   }

//   @override
//   Widget build(BuildContext context) {
//     return GrowTransiton(
//       child: FlutterLogo(),
//       animation: animation,
//     );
//   }

//   @override
//   void dispose() {
//     controller.dispose();
//     super.dispose();
//   }
// }

/// 第四种Tween.evaluate()并行加载多个动画
class AnimationPage1 extends StatefulWidget {
  @override
  _AnimationPage1State createState() => _AnimationPage1State();
}

class _AnimationPage1State extends State<AnimationPage1>
    with SingleTickerProviderStateMixin {
  AnimationController controller;
  Animation animation;
  @override
  void initState() {
    super.initState();

    controller = AnimationController(
        duration: Duration(milliseconds: 2000), vsync: this);
    animation = CurvedAnimation(parent: controller, curve: Curves.easeIn);
    controller.forward();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: AnimationLogo(
        animation: animation,
      ),
    );
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}

class AnimationLogo extends AnimatedWidget {
  final _opacityTween = Tween(begin: 0.1, end: 1.0);
  final _sizeTween = Tween(begin: 0.0, end: 300.0);

  AnimationLogo({Key key, Animation animation})
      : super(key: key, listenable: animation);
  @override
  Widget build(BuildContext context) {
    final Animation animation = listenable;
    return Center(
        child: Opacity(
      opacity: _opacityTween.evaluate(animation),
      child: Container(
        width: _sizeTween.evaluate(animation),
        height: _sizeTween.evaluate(animation),
        child: FlutterLogo(),
      ),
    ));
  }
}
